from app import db
from datetime import datetime

class AlarmRule(db.Model):
    __tablename__ = 'alarm_rules'
    
    id = db.Column(db.Integer, primary_key=True)
    point_id = db.Column(db.Integer, db.ForeignKey('monitor_points.id'), nullable=False)
    parameter = db.Column(db.String(32), nullable=False)  # 参数名称
    min_value = db.Column(db.Float)  # 最小值
    max_value = db.Column(db.Float)  # 最大值
    level = db.Column(db.String(16), nullable=False)  # 报警级别：warning/danger
    enabled = db.Column(db.Boolean, default=True)  # 是否启用
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    point = db.relationship('MonitorPoint', backref='alarm_rules')

class AlarmRecord(db.Model):
    __tablename__ = 'alarm_records'
    
    id = db.Column(db.Integer, primary_key=True)
    point_id = db.Column(db.Integer, db.ForeignKey('monitor_points.id'), nullable=False)
    rule_id = db.Column(db.Integer, db.ForeignKey('alarm_rules.id'), nullable=False)
    parameter = db.Column(db.String(32), nullable=False)
    current_value = db.Column(db.Float, nullable=False)
    threshold = db.Column(db.String(64), nullable=False)  # 阈值描述
    level = db.Column(db.String(16), nullable=False)
    status = db.Column(db.String(16), default='active')  # active/resolved
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    resolved_at = db.Column(db.DateTime)
    
    point = db.relationship('MonitorPoint', backref='alarm_records')
    rule = db.relationship('AlarmRule', backref='alarm_records') 